package com.zzj.utils;


import java.net.URLDecoder;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Utils {


    /**
     * 获取当前时间到凌晨的秒数
     * @return
     */
    public static  Long getSecondsNextEarlyMorning(){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,1);
        calendar.add(Calendar.HOUR_OF_DAY,0);
        calendar.add(Calendar.SECOND,0);
        calendar.add(Calendar.MINUTE,0);
        calendar.add(Calendar.MILLISECOND,0);
        return (calendar.getTimeInMillis()-System.currentTimeMillis())/1000;
    }


    /**
     * 生成登录码
     * @param codeLength
     * @return
     */
    public static String generateRandomLoginCode(int codeLength) {
        // 生成登录码的字符集
        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random rand = new Random();

            StringBuilder code = new StringBuilder();
            for (int i = 0; i < codeLength; i++) {
                int randomIndex = rand.nextInt(characters.length());
                code.append(characters.charAt(randomIndex));
            }
        return code.toString();
    }

    /**
     * 验证码生成
     */
    public static String getCode(int num){
        Random random=new Random();
        String code="";
        for (int i =0;i<num;i++){
            int rand= random.nextInt(10);
            code+=rand;
        }
        return code;
    }

    public static Map<String, String> parseQueryString(String queryString) {
        Map<String, String> queryParams = new HashMap<>();
        String[] pairs = queryString.split("&");

        for (String pair : pairs) {
            String[] keyValue = pair.split("=");
            if (keyValue.length == 2) {
                String key = URLDecoder.decode(keyValue[0]);
                String value = URLDecoder.decode(keyValue[1]);
                queryParams.put(key, value);
            }
        }

        return queryParams;
    }
}
